(function(){
    //发送ajax请求
    var pid=location.search.split("=")[1];
   //console.log(pid)
    ajax({
      url:"http://localhost:3000/product/getproduct?pid="+pid,//user是服务器中挂载""中的路径名称
      type:"get",
      //data:,
      dataType:"json" //自动JSON.parse()
    })//return Promise
      //  onreadystatechange中: 
      //           result <- 收到服务端的
      //  然后把result放入
      //              ↓
      //      open(result)
      //              ↓
    .then(function(result){
    //console.log(result[0]);
        var product=result[0]//不同id不同的产品
        var date;
        switch(product.debt_maturity){
            case "10天" :
                date=10;
                break;
            case "18天" :
                date=18;
                break;  
            case "1个月" :  
                date=30;
                break; 
            case "3个月" :  
                date=90;
                break;  
            case "6个月" :  
                date=180;
                break;   
            case "12个月" :  
                date=360;
                break;                    
        }
       // console.log(date)
        var table=document.getElementsByTagName("table")[0];//找到table
        var tbody=document.createElement("tbody");
        if(product.family_name=="智投宝"){
            var holeRate=parseInt(product.blending_rate)+parseInt(product.elending_rate)//综合利率为两者相加
            tbody.innerHTML=` <tr>
            <td>产品名称：</td>
            <td id="pname">${product.pname}</td>
            </tr>
            <tr>
                <td>借款利率：</td>
                <td>${product.blending_rate}% +${product.elending_rate}%</td>
                <td>综合利率<span id="hrate">${holeRate}%</span></td>
            </tr>
            <tr>
                <td >债权期限：</td>
                <td id="tdate">${product.debt_maturity}</td>
            </tr>
            <tr>
                <td>投资金额：</td>
                <td><button id='reduce'>&nbsp;-&nbsp;</button><span id="buyAccount">${product.minlend_money}</span>元<button id='add'>&nbsp;+&nbsp;</button></td>
            </tr>
           
            <tr>
                <td></td>
                <td><input type="text" placeholder="手输，最高100000" id='inputAccount' min='100' max='1000000'><span>&nbsp;元</span></td>
            </tr>
            <tr>
                <td>到期本息：</td>
                <td><span id="income">${((date*holeRate/36000+1)*product.minlend_money).toFixed(2)}</span>元</td>
            </tr>
            <tr>
                <td>可用卡券：</td>
                <td>0.00元</td>
            </tr>`;
            table.appendChild(tbody);
        }else if(product.family_name=="安心宝"){
            var  holeRate=parseInt(product.blending_rate)
            tbody.innerHTML=` <tr>
            <td>产品名称：</td>
            <td id="pname">${product.pname}</td>
            </tr>
            <tr>
                <td>借款利率：</td>
                <td>${product.blending_rate}%</td>
                <td>综合利率<span id="hrate">${product.blending_rate}%</span></td>
            </tr>
            <tr>
                <td>债权期限：</td>
                <td id="tdate">${product.debt_maturity}</td>
            </tr>
            <tr>
                <td>投资金额：</td>
                <td><button id='reduce'>&nbsp;-&nbsp;</button><span id="buyAccount">${product.minlend_money}</span>元<button id='add'>&nbsp;+&nbsp;</button></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="text" placeholder="手输，最高100000" id='inputAccount' min='100' max='1000000'><span>&nbsp;元</span></td>
            </tr>
            <tr>
                <td>到期本息：</td>
                <td><span id="income">${(((date/360)*holeRate/100+1)*product.minlend_money).toFixed(2)}</span>元</td>
            </tr>
            <tr>
                <td>可用卡券：</td>
                <td>0.00元</td>
            </tr>
            `;
            table.appendChild(tbody);
        }
        var reduce=document.getElementById("reduce");
        var add=document.getElementById("add")
        var buyAccount=document.getElementById("buyAccount")
        var inputAccount=document.getElementById("inputAccount")
        var income=document.getElementById("income");
        var hrate=parseInt(document.getElementById("hrate").innerHTML);
        var tdate=parseInt(document.getElementById("tdate").innerHTML);
        //console.log(reduce,add,buyAccount,inputAccount)
        reduce.onclick=function(){
            if(buyAccount.innerHTML>=100){
            buyAccount.innerHTML-=100;
            income.innerHTML=(((tdate/360)*hrate/100+1)*buyAccount.innerHTML).toFixed(2);            
            }
        }
        add.onclick=function(){
            buyAccount.innerHTML-=-100;
            income.innerHTML=(((tdate/360)*hrate/100+1)*buyAccount.innerHTML).toFixed(2); 
        }
        inputAccount.onblur=function(){
            var Account=inputAccount.value;
            if(Account!=""){
            if(Account<=0){
                buyAccount.innerHTML=0;
            }else if(Account>=100000){
                buyAccount.innerHTML=100000;
                income.innerHTML=(((tdate/360)*hrate/100+1)*buyAccount.innerHTML).toFixed(2); 
            }else{
                buyAccount.innerHTML=Account;
                income.innerHTML=(((tdate/360)*hrate/100+1)*buyAccount.innerHTML).toFixed(2); 
            }}
        }
    })

    ajax({
        url:"http://localhost:3000/useraccount/getinformation",//user是服务器中挂载""中的路径名称
        type:"get",
        //data:,
        dataType:"json" //自动JSON.parse()
      }).then(function(result){
          //console.log(result)
          if(result.code==1){
            var money=result.msg[0].userAccount;
            var account=document.getElementById("myAccount");
            account.innerHTML=money;
          }
        })
    var buyIt=document.getElementById("buyIt")
    buyIt.onclick=function(){
        //验证是否登录
        ajax({
            url:"http://localhost:3000/user/check",
            type:"get",
            //data:,
            dataType:"json" //自动JSON.parse()
          }).then(function(result){
                console.log(result)
                if(result.length>0){
                    var buyAccount=parseFloat(document.getElementById("buyAccount").innerHTML)
                        console.log(buyAccount)
                        confirm(`确定要买${buyAccount}元的本产品吗？`)
                        var allMoney=parseFloat(document.getElementById("myAccount").innerHTML)
                        console.log(allMoney)
                        if(buyAccount>allMoney){
                            alert("余额不足，请先联系我司充值")
                        }else{
                            var SurplusMoney=allMoney-buyAccount
                            //console.log(SurplusMoney)
                            ajax({
                                url:"http://localhost:3000/useraccount/update",//user是服务器中挂载""中的路径名称
                                type:"post",
                                data:'updatedAccount='+SurplusMoney,
                                dataType:"json" //自动JSON.parse()
                                }).then(function(result){
                                    if(result.code==0){
                                        alert(result.msg);
                                        return;
                                    }
                                    if(result==200){
                                    alert("购买成功！！！")
                                    window.location.reload();
                                    }
                            });
                            var buyAccount=parseFloat(document.getElementById("buyAccount").innerHTML);
                            var pname=document.getElementById("pname").innerHTML;
                            var tdate=document.getElementById("tdate").innerHTML;
                            var hrate=document.getElementById("hrate").innerHTML;
                            var buydata="buyAccount="+buyAccount+"&pname="+pname+"&tdate="+tdate+"&hrate="+hrate
                            ajax({
                                url:"http://localhost:3000/user_productRouter/add",//user是服务器中挂载""中的路径名称
                                type:"post",
                                data:buydata,
                                dataType:"json" //自动JSON.parse()
                                }).then(function(result){
                                    console.log(result)
                            })

                        }
                }else{
                    alert("请先登录！！！")
                }
             })      
    }    
  })()